// pages/changeCardPaw/changeCardPaw.js
var app = getApp();
var Zan = require('../../css/index')

Page(Object.assign({}, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    oldPaw:'',
    oldFocus:false,
    newPaw:'',
    newFocus: false,    
    confirmPaw:'',
    confirmFocus: false,
    src:'',
    vcode:'',
    vcodeFocus: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ src: app.globalData.baseUrl + '/getPic?' + Math.random() * 1000 })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  submit:function(e){
    var that = this;    
    let value = e.detail.value;
    let vcode = value.vcode;
    let oldPaw = value.oldPaw;
    let newPaw = value.newPaw;
    let confirmPaw = value.confirmPaw;
    if (oldPaw == '') {
      this.showZanTopTips('请输入一卡通旧密码');
      this.setData({oldFocus:true});
      return;
    }
    this.setData({ oldFocus: false });    
    if (oldPaw.length!=6) {
      this.showZanTopTips('一卡通密码必须为6位');
      this.setData({ oldFocus: true });
      return;
    }
    this.setData({ oldFocus: false });    
    if (newPaw == '') {
      this.showZanTopTips('请输入一卡通新密码');
      this.setData({ newFocus: true });
      return;
    } 
    this.setData({ newFocus: false });
    if (newPaw.length != 6) {
      this.showZanTopTips('一卡通密码必须为6位');
      this.setData({ newFocus: true });
      return;
    }
    this.setData({ newFocus: false });
    if (confirmPaw == '') {
      this.showZanTopTips('请再次输入一卡通新密码');
      this.setData({ confirmFocus: true });
      return;
    }
    this.setData({ confirmFocus: false });
    if(newPaw!=confirmPaw){
      this.showZanTopTips('两次输入的新密码不一致');
      this.setData({newPaw:'',confirmPaw:'',newFocus:true});
      return;
    }
    if (vcode.length != 4) {
      this.showZanTopTips('请填写完整验证码');
      this.setData({ vcodeFocus: true });
      return;
    }
    this.setData({ vcodeFocus: false });
    var that=this;
    var sendData={
      oldPaw:oldPaw,
      newPaw:newPaw,
      vcode:vcode,
      openid:app.globalData.openid
    }
    wx.request({
      url: app.globalData.baseUrl+'/changeCardPaw',
      data:sendData,
      method:'POST',
      success:function(res){
        let data=res.data;
        if(data['code']==200){
          wx.showToast({
            title: '修改密码成功',
          });
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })} ,1500)
        }
        else{
          that.showZanTopTips('旧密码或验证码错误')
          that.setData({
            src: app.globalData.baseUrl + '/getPic?' + Math.random() * 1000 ,
            vcode:'',
            newPaw:'',
            oldPaw:'',
            confirmPaw:'',
            newFocus: '',
            oldFocus: '',
            confirmFocus: '',          
          })
        }
      }
    })
  }
}))